package com.farm.wlp.events.impl;

import com.farm.core.page.ViewMode;
import com.farm.core.time.TimeTool;
import com.farm.parameter.FarmParameterService;
import com.farm.util.spring.BeanFactory;
import com.farm.wlp.events.WlpEventInter;
import com.farm.wuc.events.WucEventInfo;
import com.farm.wuc.events.WucEventModle;
import com.farm.wuc.events.WucEventUsers;
import com.wuc.client.WucEventClient;
import com.wuc.client.domain.EventData;

public class WlpHandler implements WlpEventInter {
	
	//处理事件的业务回调（如课程学习完成后的专业学习记录更新等）
	private static WlpEventInter wlpAppEventHandle = (WlpEventInter) BeanFactory.getBean("wlpAppEventHandle");
	
	private WucEventClient getWucClient() {
		WucEventClient client = new WucEventClient(
				FarmParameterService.getInstance().getParameter("config.wuc.api.base.url"),
				FarmParameterService.getInstance().getParameter("config.wuc.api.secret"),
				FarmParameterService.getInstance().getParameter("config.wuc.api.loginname"),
				FarmParameterService.getInstance().getParameter("config.wuc.api.password"));
		return client;
	}

	@Override
	public void runEvent(final WucEventModle modle, final WucEventInfo info, final WucEventUsers users) {
		//先处理业务回调（同步执行）
		wlpAppEventHandle.runEvent(modle, info, users);
		if (FarmParameterService.getInstance().getParameterBoolean("config.wuc.event.able")) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					WucEventClient client = getWucClient();
					String sysid = FarmParameterService.getInstance().getParameter("config.wuc.sysid");
					EventData dat = new EventData(info.getObjid(), info.getObjname(), modle.getKey(), modle.getTitle(),
							null, sysid, TimeTool.getTimeDate14(), info.getValues());
					client.recordEvent(dat, users.getEventRUsers());
					
				}
			}) {
			}.start();
		}
	}

}
